﻿using System.Collections.Generic;
using Common;
using Common.CustomException;
using Common.ParameterLocator;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using NUnit.Framework;

namespace SpecFlowTfsConnector.Test
{
    [TestFixture]
    public class TfsProjectCollectorTest : UnitTestInitialization
    {
        #region Setup/Teardown

        [SetUp]
        public new void Initialize()
        {
            base.Initialize();
            ServiceParameter.Instance.Parameter.TfsServerUrl = TestResource.TfsServerUrl;
            _tfsConnector = new TfsConnector();
        }

        #endregion

        private TfsConnector _tfsConnector;
        private const string BadWorkitemType = "Bad WorkItemType";
        private const string BadTfsProjectName = "BadSpecFlowLinker";

        [Test]
        public void CanFailWhenProjectDoesntExistInTtfs()
        {
            Assert.Throws(
                Is.TypeOf<TfsException>().And.Message.EqualTo(string.Format(Resource.PROJECT_NOT_DECLARED_IN_TFS,
                                                                            BadTfsProjectName)),
                () => _tfsConnector.GetTfsUserStoriesWorkItem(BadTfsProjectName));
        }

        [Test]
        public void CanFailWhenTfsQueryIsWrongTfs()
        {
            ServiceParameter.Instance.Parameter.TfsQueryUserStory = "Select [System.Oupss] From WorkItems";
            _tfsConnector = new TfsConnector();
            Assert.Throws(
                Is.TypeOf<TfsException>().And.Message.EqualTo(string.Format(Resource.BAD_TFS_QUERY,
                                                                            ServiceParameter.Instance.Parameter.
                                                                                TfsQueryUserStory)),
                () => _tfsConnector.GetTfsUserStoriesWorkItem(TestResource.TfsProjectName));
        }

        [Test]
        public void CanFailWhenTfsQueryIsntProvidedInVisualStudio()
        {
            ServiceParameter.Instance.Parameter.TfsQueryUserStory = string.Empty;
            _tfsConnector = new TfsConnector();
            Assert.Throws(
                Is.TypeOf<VisualStudioParameterException>().And.Message.EqualTo(
                    Error.TfsQueryIsNotProvided),
                () => _tfsConnector.GetTfsUserStoriesWorkItem(TestResource.TfsProjectName));
        }

        [Test]
        public void CanFailWhenWorkItemTypeIsntProvidedInVisualStudio()
        {
            ServiceParameter.Instance.Parameter.TfsWorkitemType = string.Empty;
            _tfsConnector = new TfsConnector();
            Assert.Throws(
                Is.TypeOf<VisualStudioParameterException>().And.Message.EqualTo(
                    Error.WorkItemTypeIsNotProvided),
                () => _tfsConnector.GetTfsUserStoriesWorkItem(TestResource.TfsProjectName));
        }

        [Test]
        public void CanFailWhenWorkItemTypeIsntRecognizedInTheProject()
        {
            ServiceParameter.Instance.Parameter.TfsWorkitemType = BadWorkitemType;
            _tfsConnector = new TfsConnector();
            Assert.Throws(
                Is.TypeOf<TfsException>().And.Message.EqualTo(
                    Error.ProjectDoesNotContainThisWorkItemType),
                () => _tfsConnector.GetTfsUserStoriesWorkItem(TestResource.TfsProjectName));
        }

        [Test]
        public void CanGetAllProjectFromTfsServer()
        {
            ProjectCollection projects = _tfsConnector.GetProjects();
            Assert.IsNotNull(_tfsConnector);
            Assert.IsNotNull(projects);
        }

        [Test]
        public void CanGetAllUserStoryFromTfsServer()
        {
            WorkItemCollection userStories = _tfsConnector.GetTfsUserStoriesWorkItem(TestResource.TfsProjectName);
            Assert.IsNotNull(userStories);
        }

        [Test]
        public void CanGetLinkedItemFromWorkITem()
        {
            WorkItemCollection userStories = _tfsConnector.GetTfsUserStoriesWorkItem(TestResource.TfsProjectName);
            foreach (WorkItem item in userStories)
            {
                List<WorkItem> linkedItems = TfsConnector.GetTestsCaseWorkItemByUserStory(item);
                Assert.IsNotNull(linkedItems);
            }
        }
    }
}